#!/usr/bin/perl

#system("export NF2_DESIGN_DIR=/root/NF2/projects/packet_generator");

#folders
$root_folder="/root/switch_project";
$result_folder="$root_folder/experiment/exp19"; #root dir to store experimental resutls
$aggregated_result="$result_folder/total_result.txt"; #results for all experiments, used for plot

#input traffic file for netFPGA
$inpcap="$root_folder/traffic/G4/n1_to_e1_1500.pcap"; #flow1
#$inpcap_flow2="$root_folder/traffic/G4/n2_to_e1_1500.pcap"; #flow2

$runs=1; # number of repeated experiment (same setting)
$iteration=10; #number of iteration for packet transmission in netFPGA


#used to calculate gap (not used now)
$packet_size=1500; #byte
$number_of_packets=10*$iteration; #because nf2_dag1.pcap has 10 packets
@speed;
@gap;

#specify sending rate
$start_sending_rate=900;#Mb/s
$end_sending_rate=1000;
$sending_rate_gap=100;

#calculate speed and gap (gap is not used for now)
for($b=$start_sending_rate, $a=0; $b<=$end_sending_rate; $b+=$sending_rate_gap, $a++)
{
	$speed[$a]=$b;
	#$gap[$a]=getInterFrameGap($speed[$a], $packet_size, $number_of_packets);
	print "\n speed = $speed[$a]\n";
}


#disable broadcast
system("dagflood -f $root_folder/traffic/G4/e1_to_n1.erf -d dag0 -c 1");


#run experiment
#j-control sending rate from netfpga to dag1, 
#i-control runs of experiments for same setting
for($j=0; $j<=$#speed; $j++)
{
	for($i=1; $i<=$runs; $i++)
	{
#create folder according to sending rate and index of run
		print("$result_folder/$speed[$j]M/$i\n");
		mkdir "$result_folder/$speed[$j]M";
		mkdir "$result_folder/$speed[$j]M/$i";
		chdir "$result_folder/$speed[$j]M/$i";

		system("dagsnap -d0:0 -o 1.erf &");
		system("dagsnap -d0:2 -o 2.erf &");
		$rate = $speed[$j]*1000;
		system("packet_generator.pl -q0 $inpcap -r0 $rate -i0 $iteration -q2 $inpcap -r2 $rate -i2 $iteration");

#clean up
		system("killall dagsnap");
		sleep 2;
	}
}

#post-process data
for($j=0; $j<=$#speed; $j++)
{
	for($i=1; $i<=$runs; $i++)
	{
		chdir "$result_folder/$speed[$j]M/$i";
#processing erf
		system("dagbits -vf 1.erf decode |grep ts=0x > 1erf.tim");
		system("dagbits -vf 2.erf decode |grep ts=0x > 2erf.tim");

#extract timestamp
		compute_timestamp("1erf.tim", "1.tim");
		compute_timestamp("2erf.tim", "2.tim");

#calculate delay
		system("$root_folder/script/get_delay.pl 2.tim 1.tim delay.txt result.txt $aggregated_result");

#clean up
		#system("rm -f 1erf.tim");
		#system("rm -f 2erf.tim");
	}
}

chdir "$root_folder/script";


#arguments: 1. sending rate(Mb/s), 2. packet size(Byte) 3. number of packets
sub getInterFrameGap
{
	my($sending_rate)=$_[0];
	my($pk_size)=$_[1];
	my($num_of_packets)=$_[2];	
	#print("$sending_rate, $pk_size, $number_of_packets\n");

	my($bandwidth)=10**9;
	my($interframe_gap); #ns
	my($formatted_ifg);

	$interframe_gap=(($pk_size*8*$num_of_packets/$sending_rate/(10**6))-($pk_size*8/$bandwidth))/($num_of_packets-1) * (10**9);
	#print("interframe gap = $interframe_gap\n");
	
	$formatted_ifg=sprintf("%.0f", $interframe_gap);
	return $formatted_ifg;
}

#only up to day, be careful when doing experiment in midnight :)
#argument 1. input file after dagbits 2. output file
sub compute_timestamp
{
	my($in)=$_[0];
	my($out)=$_[1];

	open(IN, $in) or die "$in cannot be opened.\n";
	open(OUT, ">$out") or die "$out cannot be opened.\n";

	while(<IN>)
	{
		my($line)=$_;
		chomp($line);

		@block1=split(' ', $line);
		#print "$block1[2]\n";
		@block2=split(':', $block1[2]);
		#print "$block2[0] $block2[1] $block2[2]\n";
		$time = $block2[2]+$block2[1]*60+$block2[0]*3600;
		print OUT "$time\n";
		#print "$time\n";
	}

	close(IN);
	close(OUT);
}
